#include "IceLayer.h"

#include "Camera.h"
#include "Physics.h"
#include "Player.h"
#include "Events\CSGD_EventSystem.h"
#include "Time.h"

vector<IceLayer::SlidingObject> IceLayer::slidingObjects;
int IceLayer::iceCount = 0;
int IceLayer::iceCounter = 0;

// Constructor
IceLayer::IceLayer(void)
{
	objectID = "Ice Layer";
	isFrozen = true;
	slideSpeed = 50.0f;

	SetMinTemp(-3.0f);
	SetMaxTemp(3.0f);
	SetCurTemp(MinTemp());

	++iceCount;
}

// Destructor
IceLayer::~IceLayer(void)
{
	--iceCount;
	slidingObjects.clear();
}

// Update
void IceLayer::Update(float deltaTime)
{
	if(CurTemp() < 0.0f)
		isFrozen = true;
	else
		isFrozen = false;

	if(isFrozen == false)
		return;

	for(unsigned int i = 0; i < slidingObjects.size(); ++i)
	{
		if(slidingObjects[i].collided == 0)
		{
			if(slidingObjects[i].object->GetObjectID() == "Player")
			{
				Player* player = dynamic_cast<Player*>(slidingObjects[i].object);

				if(player != nullptr)
					player->ActivateMovement();
			}

			slidingObjects.erase(slidingObjects.begin() + i);

			--i;
			continue;
		}

		if(slidingObjects[i].wasMoved == true)
			continue;

		GameObject* obj = slidingObjects[i].object;

		//if(slidingObjects[i].gotDelta && slidingObjects[i].current.fX != obj->GetPosX())
			//obj->SetPosX(slidingObjects[i].current.fX);
		
		tVector2D delta = slidingObjects[i].current - slidingObjects[i].previous;
		delta.fY = 0;

		if(slidingObjects[i].gotDelta == false && Vector2DLength(delta) > 1.0f)
		{
			delta = Vector2DNormalize(delta);
			slidingObjects[i].delta = delta.fX;
			slidingObjects[i].gotDelta = true;

			if(obj->GetObjectID() == "Player")
			{
				Player* player = dynamic_cast<Player*>(obj);

				if(player != nullptr)
					player->DeactivateMovement();
			}
		}

		obj->SetPosX(obj->GetPosX() + slidingObjects[i].delta * slideSpeed * deltaTime);
		slidingObjects[i].collided--;
		slidingObjects[i].wasMoved = true;

		slidingObjects[i].previous = slidingObjects[i].current;

		slidingObjects[i].current.fX = obj->GetPosX();
		slidingObjects[i].current.fY = obj->GetPosY();
	}

	++iceCounter;

	if(iceCounter == iceCount)
	{
		for(unsigned int i = 0; i < slidingObjects.size(); ++i)
			slidingObjects[i].wasMoved = false;
		iceCounter = 0;
	}
}

// Render
void IceLayer::Render(void) const
{
	int width = hitBox.right - hitBox.left;
	int height = hitBox.bottom - hitBox.top;
	int y = (int)(height * (1 - (CurTemp() / MinTemp())));

	RECT sect;
	sect.left = 0;
	sect.right = width;
	sect.top = y;
	sect.bottom = height;

	CSGD_TextureManager::GetInstance()->Draw(
		puddleImageID,
		hitBox.left - (int)Camera::GetInstance()->OffsetX(),
		hitBox.top - (int)Camera::GetInstance()->OffsetY()
		);

	CSGD_TextureManager::GetInstance()->Draw(
		imageID,
		(int)(hitBox.left - Camera::GetInstance()->OffsetX()),
		(int)(hitBox.top - Camera::GetInstance()->OffsetY()) + y,
		1.0f, 1.0f, &sect);
}

// HandleCollision
void IceLayer::HandleCollision(GameObject& other, CollisionInfo& info)
{
	if(isFrozen == false)
		return;

	if(info.Side == CollisionSide::BOTTOM)
		other.SetPosY(other.GetPosY() - (info.Overlap.bottom - info.Overlap.top));
	other.SetHitBox(Physics::UpdateColRect(other.GetHitBox(), (int)other.GetPosX(), (int)other.GetPosY()));

	bool add = true;
	unsigned int i = 0;
	for(; add && i < slidingObjects.size(); ++i)
		if(slidingObjects[i].object == &other)
			add = false;

	--i;

	if(add == true)
	{
		SlidingObject obj;
		obj.object = &other;
		obj.current.fX = obj.previous.fX = other.GetPosX();
		obj.current.fY = obj.previous.fY = other.GetPosY();
		obj.delta = 0.0f;
		obj.collided = Time::GetInstance().FPS() >> 4;
		obj.gotDelta = false;

		slidingObjects.push_back(obj);
	}
	else if(i >= 0)
	{
		slidingObjects[i].collided = Time::GetInstance().FPS() >> 4;
	}
	else if(i == -1)
	{
		int MYINT = 0;
		MYINT = INT_MAX;
	}
}

// ResetToCheckPoint
void IceLayer::ResetToCheckPoint()
{

}

void IceLayer::SetPuddleImageID(int id)
{
	puddleImageID = id;
}

void IceLayer::SetSlideSpeed(float speed)
{
	slideSpeed = speed;
}